// pages/opreation/studentAssignment/student/index.js
import {
  request
} from '../../../../request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentList: [],
    modalName: null,
    idCache:null,
    currentData:[],
    modelList:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData(options.id);
  },
  initData(id) {
    request("/gym/studentAssignment/student/list/"+id, "GET").then(resopnse => {
      this.setData({
        studentList: resopnse.students,
        idCache:id
      });
    })
  },
  selectStudent(e) {
    const { value } = e.detail
    const data = this.data.currentData
    const index = data.indexOf(value)
    const current =
      index === -1 ? [...data, value] : data.filter((n) => n !== value)

    this.setData({
      currentData: current,
    })
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  pageBack() {
    wx.navigateBack({
      delta: 1
    });
  }, // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },
  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  showModal(e) {
    if(e.currentTarget.dataset.target=="student"){
      request("/gym/studentAssignment/student/list/", "GET").then(resopnse => {
        this.setData({
          modelList : resopnse.students
        });
      })
    }
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  delete(e) {
    var that = this;
    var id =  this.data.idCache;
    var item = e.currentTarget.dataset['item'];
    wx.showModal({
      title: '提示',
      content: '是否删除当前商品',
      success(res) {
        if (res.confirm) {
          request("/gym/studentAssignment/delete/" + item.memberId, "GET").then(res => {
            if (res.code == "200") {
              wx.showToast({
                title: '删除成功',
                icon: '',
                duration: 2000,
              })
              that.initData(id);
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000,
              })
            }
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  submit(){
    var student =  this.data.currentData;
    var reqStr = "/gym/studentAssignment/update?teacherId="+this.data.idCache+"&students=";
    for(var i = 0;i<student.length;i++){
      reqStr+=student[i];
      if(i!=student.length-1){
        reqStr+=",";
      }
    }
    request(reqStr, "PUT").then(resopnse => {
      this.setData({
        modalName: null
      })
      this.initData(this.data.idCache);
    })
  }
})